package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.BannerModel;
import com.hangzhou.xyj.model.DianDramaModel;
import com.hangzhou.xyj.model.SingModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.ui.activity.DramaCategoryListActivity;
import com.hangzhou.xyj.ui.activity.ExpertListActivity;
import com.hangzhou.xyj.ui.activity.SingingListActivity;
import com.hangzhou.xyj.ui.widget.FamousExpertBannerView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DT-038 on 2015/12/22.
 * 名家adapter
 */
public class DianDramaStageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private RecDramaItemVH recDramaItemVH;
    private BannerViewHolder bannerViewHolder;

    public DianDramaStageAdapter(Context context) {
        this.mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case 0:
                viewHolder = new BannerViewHolder(mLayoutInflater.inflate(R.layout.layout_banner, viewGroup, false));
                break;
            case 1:
                viewHolder = new CategoryHolder(mLayoutInflater.inflate(R.layout.layout_dian_drama_category, viewGroup, false));
                break;
            case 2:
                viewHolder = new RecDramaTitleVH(mLayoutInflater.inflate(R.layout.layout_dian_drama_recommed_title, viewGroup, false));
                break;
            case 3:
                viewHolder = new RecDramaItemVH(mLayoutInflater.inflate(R.layout.layout_dian_drama_recommed_grid, viewGroup, false));
                break;
            default:
                viewHolder = new RecyclerView.ViewHolder(new View(mContext)) {
                };
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder == null) {
            return;
        }
        if (viewHolder instanceof BannerViewHolder) {
            (bannerViewHolder = (BannerViewHolder) viewHolder).setData(new ArrayList<BannerModel>());
        } else if (viewHolder instanceof CategoryHolder) {
            ((CategoryHolder) viewHolder).setData();
        } else if (viewHolder instanceof RecDramaItemVH) {
            (recDramaItemVH = ((RecDramaItemVH) viewHolder)).updateData(null);
        }

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    /**
     * 更新推荐曲目数据
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/11 14:32
     */
    public void updateRecommendDramaData(List<SingModel> datas) {
        recDramaItemVH.updateData(datas);
    }

    /**
     * 更新Banner数据
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/11 16:44
     */
    public void updateBannerData(List<BannerModel> datas) {
        bannerViewHolder.setData(datas);
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        private FamousExpertBannerView mItemView;

        public BannerViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View view) {
            mItemView = (FamousExpertBannerView) view.findViewById(R.id.view_famous_expert_banner);
        }

        public void setData(List<BannerModel> list) {
            mItemView.setData(list);
        }
    }

    class CategoryHolder extends RecyclerView.ViewHolder {
        private final String[] categoryArray = {
                "名家点戏",
                "分类点戏",
                "热门点唱",
                "独自清唱",
        };

        private LinearLayout mItemView;

        private View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击的item位置
                //点击的View置为选中
//                view.findViewById(R.id.iv_category_item).setSelected(true);
//                TextView tv = (TextView) view.findViewById(R.id.mtv_category_item);
//                tv.setTextColor(mContext.getResources().getColor(R.color.common_white));
//                //其他置为未选中
//                for (int i = 0; i < mItemView.getChildCount(); i++) {
//                    View childView = mItemView.getChildAt(i);
//                    if (childView == view) {
//                        continue;
//                    }
//                    childView.findViewById(R.id.iv_category_item).setSelected(false);
//                    TextView tv1 = (TextView) childView.findViewById(R.id.mtv_category_item);
//                    tv1.setTextColor(mContext.getResources().getColor(R.color.common_purple));
//                }

                //跳转
                int indexOfChild = mItemView.indexOfChild(view);
                Intent intent;
                switch (indexOfChild) {
                    case 0: //"名家点戏"
                        intent = new Intent(mContext, ExpertListActivity.class);
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_DIAN_DRAMA_EXPERT.getValue());
                        intent.putExtra(DataKey.TITLE, "名家点戏");
                        IntentManager.getInstance().goExpertListActivity(mContext, intent);
                        break;
                    case 1: //"分类点戏"
                        intent = new Intent(mContext, DramaCategoryListActivity.class);
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_DIAN_DRAMA_CATEGORY.getValue());
                        intent.putExtra(DataKey.TITLE, "分类点戏");
                        IntentManager.getInstance().goExpertListActivity(mContext, intent);
                        break;
                    case 2://"热门点唱"
                        intent = new Intent(mContext, SingingListActivity.class);
                        intent.putExtra(DataKey.TITLE, "热门点唱");
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_DIAN_DRAMA_HOT_SING.getValue());
                        IntentManager.getInstance().goActivity(mContext, intent);
                        break;
                    case 3://"独自清唱"
                        IntentManager.getInstance().goSingAloneActivity(mContext);
                        break;
                }

            }
        };

        public CategoryHolder(View itemView) {
            super(itemView);
            this.mItemView = (LinearLayout) itemView;
            initViews();
        }

        private void initViews() {
            for (String category : categoryArray) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
                params.gravity = Gravity.CENTER;
                mItemView.addView(createChildView(category), params);
            }
        }

        private View createChildView(String category) {
            View childView = View.inflate(mContext, R.layout.layout_dian_drama_category_item, null);
            MyTextView textView = (MyTextView) childView.findViewById(R.id.mtv_category_item);
            textView.setText(category);
            childView.setOnClickListener(listener);
            return childView;
        }

        public void setData() {

        }
    }

    /**
     * 推荐曲目标题
     */
    class RecDramaTitleVH extends RecyclerView.ViewHolder {

        public RecDramaTitleVH(View itemView) {
            super(itemView);
        }
    }

    /**
     * 推荐曲目item
     */
    class RecDramaItemVH extends RecyclerView.ViewHolder {
        private GridView mGridView;
        private RecDramaAdapter mAdapter;

        public RecDramaItemVH(View itemView) {
            super(itemView);
            mGridView = (GridView) itemView.findViewById(R.id.grid_view);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    SingModel model = mAdapter.getItem(position);
                    DianDramaModel dramaModel = null;
                    if (model != null) {
                        dramaModel = new DianDramaModel();
                        dramaModel.setId(model.getId());
                        dramaModel.setName(model.getDramaName());
                        dramaModel.setNativeUrl(model.getNativeUrl()); //原唱
                        dramaModel.setAccompanyUrl(model.getAccompanyUrl());//伴奏
                        dramaModel.setLrcUrl(model.getLrcUrl()); //歌词
                        dramaModel.setAuthor(model.getRecommendDramaName());
                        dramaModel.setHead(model.getShowImg());
                    }
                    IntentManager.getInstance().goRecordActivity(mContext, dramaModel, true);
                }
            });
        }

        public void updateData(List<SingModel> list) {
            mGridView.setAdapter(mAdapter = new RecDramaAdapter(mContext, list));
        }
    }

}
